package com.manor.main.fragment.square

import android.content.Context
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter

/**
 * created by Qin
 * on 2020/3/29
 */
class SquarePagerAdapter(fm: FragmentManager, private val mContext: Context) : FragmentPagerAdapter(fm) {
    private val tabTitles: Array<String>
    private var mSquareCityFragment: SquareCityFragment? = null
    private var mSquareAuthFragment: SquareAuthFragment? = null


    init {
        tabTitles = arrayOf("同城", "认证")
    }


    override fun getItem(position: Int): Fragment {

        when (position) {
            1 -> {
                if (mSquareAuthFragment == null) {
                    mSquareAuthFragment = SquareAuthFragment.newInstance()

                }
                return mSquareAuthFragment as SquareAuthFragment
            }
            0 -> {
                if (mSquareCityFragment == null) {
                    mSquareCityFragment = SquareCityFragment.newInstance()

                }
                return mSquareCityFragment as SquareCityFragment
            }
            else -> {
                if (mSquareCityFragment == null) {
                    mSquareCityFragment = SquareCityFragment.newInstance()

                }
                return mSquareCityFragment as SquareCityFragment
            }
        }
    }

    /**
     * 这样写不安全 先这样
     * @param position Int
     * @return CharSequence?
     */
    override fun getPageTitle(position: Int): CharSequence? {

        return tabTitles[position]
    }

    override fun getCount(): Int {
        return PAGE_COUNT
    }



    companion object {


        private val PAGE_COUNT = 2
    }

}
